package userinterface;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import db.DBoc;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;

public class BasicUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	JPanel contentPane = new JPanel();
	static Container container = new JPanel();
	
	static JFrame frame0;
	
	JButton addButton, queryButton;
	JList<String> addList = new JList<String>();
	JList<String> queryList = new JList<String>();
	
	String[] addStrings = {"addStuInfo", "addClaInfo", "addGradeInfo"},
			 queryStrings = {"queryByCla", "queryByStuID", "queryBySpec", "queryByDepart"};
	
/*--------------------------------List----------------------------------------------*/
	
	public JList<String> buildList(int x, int y, String[] ss) {

		JList<String>l = new JList<String>(ss);
		l.setVisibleRowCount(3);

        
		l.setForeground(Color.WHITE);
		l.setBackground(Color.GRAY);
		l.setBounds(x, y, 100, 100);
		
		return l;
	}
	
/*-------------------Button-----------------------------------------------------------*/
	
	public void buildButton(JButton b, String name, int x, int y, 
				JList<String> al) {
		b = new JButton(name);

		b.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				container.add(al);
				container.repaint();
				container.validate();
			}
			@Override
			public void mouseExited(MouseEvent e) {
				if (e.getX() < 80) {
					container.remove(al);
					container.repaint();
					container.validate();
				}
			}
		});
		b.setPreferredSize(new Dimension(80, 100));
		b.setForeground(Color.WHITE);
		b.setOpaque(true);
		b.setBackground(Color.DARK_GRAY);
		b.setBorderPainted(false);
		b.setBounds(x, y, 80, 100);
		container.add(b);
	}

	
/*-------------------BasicUI-----------------------------------------------------------*/
	
	public BasicUI() {
		getContentPane().setLayout(null);   
		
		setTitle("学生绩点评优管理软件");

		DBoc dBoc = new DBoc();
		dBoc.Connect();
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent we) {
				dBoc.Close();
				System.exit(0);
				
			}
		});
		setBounds(100, 100, 750, 530);
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.setBounds(100, 100, 750, 530);
		setContentPane(contentPane);
		
		container.setEnabled(false);
		container.setLayout(null);
		
		
		addList = buildList(80, 100, addStrings);
		
		addList.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				int choice = e.getLastIndex();
				dispose();
				
				switch (choice) {
				case 0:
					container = new JPanel();
					frame0 = new AddStuInfo();
					break;
				case 1:
					container = new JPanel();
					frame0 = new AddClaInfo();
					break;
				case 2:
					container = new JPanel();
					frame0 = new AddGradeInfo();
					break;
				}
				frame0.setVisible(true);
			}
		});
		queryList = buildList(80, 201, queryStrings);
		
		queryList.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				int choice = e.getLastIndex();
				dispose();
				
				switch(choice) {
					case 0:
						container = new JPanel();
						frame0 = new QueryByCla();
						break;
					case 1:
						container = new JPanel();
						frame0 = new QueryByStuID();
						break;
					case 2:
						container = new JPanel();
						frame0 = new QueryBySpec();
						break;
					case 3:
						container = new JPanel();
						frame0 = new QueryByDepart();
						break;
				}
				frame0.setVisible(true);
			}
		});
		
		buildButton(addButton, "add", 0, 100, addList);
		buildButton(queryButton, "query", 0, 201, queryList);
		
		contentPane.add(container);
	
	}

	
}

